package com.example.arcapplication.util;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

public class Utils {

    private static Utils mUtils;

    private static Context mContext;

    private Utils() {
    }

    public static Utils getInstance() {
        if (null == mUtils) {
            mUtils = new Utils();
        }
        return mUtils;
    }

    public static void init(Application application) {
        mContext = application;
    }

    public static <T> boolean isEmpty(List<T> ts) {
        return ts == null || ts.size() == 0;
    }

    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
    }

    public void showToast(int resId) {
        Toast.makeText(mContext, resId, Toast.LENGTH_LONG).show();
    }


    public static String getRequestXml(String method, Map<String, String> valuesMap, int SOAPVersion) {
        String xmlStr;
        if (SOAPVersion == Config.SOAP_1_2) {
            xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap12:Body>\n" +
                    "    <" + method + " xmlns=\"" + Config.URL_NAME_SPACE + "\">\n" +
                    "      " + getNodeValue(valuesMap) + "\n" +
                    "    </" + method + ">\n" +
                    "  </soap12:Body>\n" +
                    "</soap12:Envelope>";
        } else {
            xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap1=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap:Body>\n" +
                    "    <ibus:" + method + " xmlns=\"" + Config.URL_NAME_SPACE + "\">\n" +
                    "      " + getNodeValue(valuesMap) + "\n" +
                    "    </ibus:" + method + ">\n" +
                    "  </soap:Body>\n" +
                    "</soap:Envelope>";
        }
        return xmlStr;
    }

    private static String getNodeValue(Map<String, String> valuesMap) {
        StringBuilder str = new StringBuilder();
        for (Map.Entry<String, String> entry : valuesMap.entrySet()) {
            str.append(toStart(entry.getKey())).append(entry.getValue()).append(toEnd(entry.getKey()));
        }
        return str.toString();
    }

    private static String toStart(String key) {
        return "<" + key + ">";
    }

    private static String toEnd(String key) {
        return "</" + key + ">";
    }

    // 解析xml
    public static String analysisResult(String response, final String method) {
        String startTag = "<" + method + "Result" + ">";
        String endTag = "</" + method + "Result" + ">";
        if (response.contains(startTag) && response.contains(endTag)) {
            int startIndex = response.indexOf(startTag) + startTag.length();
            int endIndex = response.lastIndexOf(endTag);
            return response.substring(startIndex, endIndex);
        }
        return null;
    }

    public static String analysisFault(String response, final String method) {
        String startTag = "<" + method + ">";
        String endTag = "</" + method + ">";
        if (response.contains(startTag) && response.contains(endTag)) {
            int startIndex = response.indexOf(startTag) + startTag.length();
            int endIndex = response.lastIndexOf(endTag);
            return response.substring(startIndex, endIndex);
        }
        return null;
    }

    public static String getResultData(String o) {
        if (!TextUtils.isEmpty(o) && o.contains("soap:Envelope")) {
            String res = Utils.analysisFault(o, "return");
            String s = res.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
            return Utils.analysisFault(s, "data");
        }
        return null;
    }

}
